/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 13, 2012  11:27:51 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : sap.business.mm.inbound
 * File Name : MmConvFactInbService.java
 * TODO
 */
package sap.business.mm.inbound;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import jpa.entitys.synchro.common.ACTION_HANDLER;
import jpa.entitys.synchro.staging.MmConvFactInb;
import jpa.service.common.SessionEntityManager;
import jpa.service.synchro.staging.MmConvFactInbServices;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import sap.business.commons.services.CommonJCOReturn;
import apps.promis.in.common.CostArticleUmConversionAction;

import com.sap.mw.jco.JCO;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Dec 4, 2013 : 10:18:25 AM 
 * CreateBy=Chatree Siangklom
 * MmConvFactInbService
 * TODO
 */
public class MmConvFactInbService {
	
	private static final Logger log = Logger.getLogger(MmConvFactInbService.class);
	
	public void CreatedSync(JCO.ParameterList input, JCO.ParameterList tables, JCO.ParameterList output, String structureOrTableName) throws Exception {

		List<jpa.entitys.synchro.staging.MmConvFactInb> lst_object = new ArrayList<jpa.entitys.synchro.staging.MmConvFactInb>();
		JCO.Table table = tables.getTable(structureOrTableName);
		List<jpa.entitys.synchro.staging.MmConvFactInb> lst = null;
		int records = -1;
		try{
			records = table.getNumRows();
			
			EntityManager em = new SessionEntityManager().CreatedEntityManager();
			MmConvFactInbServices MmConvFactInbServ = new MmConvFactInbServices(em);
			String instance_no = MmConvFactInbServ.GetNewInstanceNo();
			
			for(int i = 0; i < records; i++)
			{
				log.info("i ="+ i);
				table.setRow(i);			
				jpa.entitys.synchro.staging.MmConvFactInb model = new jpa.entitys.synchro.staging.MmConvFactInb();
				
				//model.setPromisStagingId(CommonService.IsNullRNull(table.getString("PROMIS_STAGING_ID")));
				model.setInstanceNo(instance_no);
				model.setArticleCode(CommonService.IsNullRNull(table.getString("ARTICLE_CODE")));
				model.setUm(CommonService.IsNullRNull(table.getString("UM")));						
				
				model.setMasterUm(CommonService.IsNullRNull(table.getString("MASTER_UM")));
				
				model.setUmDesciption(CommonService.IsNullRNull(table.getString("UM_DESCRIPTION")));
				model.setMasterUmDescription(CommonService.IsNullRNull(table.getString("MASTER_UM_DESCRIPTION")));
				
				model.setNumeratorConversion(new BigDecimal(CommonService.IsNullRZero(table.getString("NUMERATOR_CONVERSION"))));
				model.setDenominatorConversion(new BigDecimal(CommonService.IsNullRZero(table.getString("DENOMINATOR_CONVERSION"))));									
				
				//Set Txn Reacord
				model.setCreatedOn(CommonService.IsNullRNull(table.getString("CREATED_ON")));
				model.setCreatedBy(CommonService.IsNullRNull(table.getString("CREATED_BY")));
				model.setUpdatedOn(CommonService.IsNullRNull(table.getString("LAST_UPD_ON")));
				model.setUpdatedBy(CommonService.IsNullRNull(table.getString("LAST_UPD_BY")));
				
				if(CommonService.IsNull(table.getString("LAST_UPD_ON")))
					model.setActionHandle(ACTION_HANDLER.INSERTED.toString());
				else 
					model.setActionHandle(ACTION_HANDLER.UPDATED.toString());
				
				//Set SAP External Key link
				model.setExtRefKey(CommonService.IsNullRNull(table.getString("SYNC_EXTERNAL_REF_KEY")));
				model.setExtRefNo(CommonService.IsNullRNull(table.getString("SYNC_EXTERNAL_REF_NO")));
				
				lst_object.add(model);
			}
			
			//Insert to Staging 
			MmConvFactInbServ.Inserted(lst_object);			 
			
			//Find By Instance
			lst = MmConvFactInbServ.findByInstanceNo(instance_no);				
			
			if(!CommonService.IsNull(lst))
			{
				//Create To Promis
				new CostArticleUmConversionAction().Start(lst);
				
				if(lst.size() == records){
					for(int i = 0; i < records; i++)
					{
						MmConvFactInb obj = lst.get(i);
						table.setRow(i);
						table.setValue(CommonService.IsNullRString(obj.getPromisStagingId()), "PROMIS_TXN_ID");
						table.setValue("S", "MSG_CODE");
						table.setValue("Success", "MSG_DESC");
						table.setValue("SAP NO =" + obj.getExtRefNo(), "MSG_ALT_DESC");
						table.setValue("", "MSG_ACTION_RESULT");	
						log.info(CommonJCOReturn.toString(table));
					}
				}
			}else
				log.info("find not found  instance no = " + instance_no);
		}
		catch(Exception e)
		{	log.error(e);
			if(!CommonService.IsNull(lst)){
					if(lst.size() == records){
						for(int i = 0; i < records; i++)
						{
							MmConvFactInb obj = lst.get(i);
							table.setRow(i);
							table.setValue(CommonService.IsNullRString(obj.getPromisStagingId()), "PROMIS_TXN_ID");
							table.setValue("E", "MSG_CODE");
							table.setValue(e.getMessage(), "MSG_DESC");
							table.setValue("SAP NO =" + obj.getExtRefNo(), "MSG_ALT_DESC");
							table.setValue("", "MSG_ACTION_RESULT");
							
							try {
								log.info(CommonJCOReturn.toString(table));
							} catch (Exception e1) {
								log.error(e1.getMessage());
								System.err.println(e1.getMessage());
							}
						}
					}			
			}else{			
				for(int i = 0; i < records; i++)
				{
					table.setRow(i);
					table.setValue("E", "MSG_CODE");
					table.setValue(e.getMessage(), "MSG_DESC");
				}
			}
		}
	}
}
